﻿using System;
using System.Deployment.Application;
using System.Linq;
using System.Net;
using System.Net.Sockets;

namespace DNEL.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class DNELMethods
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool IsDebuggerAttached()
        {
            return System.Diagnostics.Debugger.IsAttached;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="IncludeDomain"></param>
        /// <returns></returns>
        public static string GetCurrentUser(bool IncludeDomain)
        {
            string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

            if (IncludeDomain)
            {
                return user;
            }
            else
            {
                int pos = user.IndexOf('\\');
                return pos != -1 ? user.Substring(pos + 1) : user;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUserDomain()
        {
            string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            return user.Remove(user.IndexOf('\\'));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetLoggedInUser()
        {
            return Environment.UserName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetLoggedInUserDomain()
        {
            return Environment.UserDomainName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetNetworkVersion()
        {
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                return ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
            }
            else
            {
                return "0.0.0.0";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetHostName()
        {
            return System.Environment.MachineName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="AddressFamily"></param>
        /// <returns></returns>
        public static IPAddress GetLocalIP(AddressFamily AddressFamily)
        {
            return Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static IPAddress GetLocalIPv4()
        {
            return Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static IPAddress GetLocalIPv6()
        {
            return Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetworkV6);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetWindowsVersion(bool Detailed)
        {
            if (Detailed)
            {
                return System.Environment.OSVersion.VersionString;
            }
            else
            {
                int major = System.Environment.OSVersion.Version.Major;
                int minor = System.Environment.OSVersion.Version.Minor;

                if (major == 4 && minor == 0) return "Win95";
                if (major == 4 && minor == 10) return "Win98";
                if (major == 4 && minor == 90) return "WinMe";
                if (major == 5 && minor == 0) return "Win2000";
                if (major == 5 && minor == 1) return "WinXP";
                if (major == 6 && minor == 0) return "Vista";
                if (major == 6 && minor == 1) return "Win7";
                if (major == 6 && minor == 2) return "Win8";
                if (major == 6 && minor == 3) return "Win8.1";
                return "unknown";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Length"></param>
        /// <returns></returns>
        public static string GetRandomString(int Length)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Length"></param>
        /// <param name="UsableChars"></param>
        /// <returns></returns>
        public static string GetRandomString(int Length, params string[] UsableChars)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Year"></param>
        /// <returns></returns>
        public static DateTime GetDaylightTimeStart(int Year)
        {
            return TimeZone.CurrentTimeZone.GetDaylightChanges(Year).Start;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Year"></param>
        /// <returns></returns>
        public static DateTime GetDaylightTimeEnd(int Year)
        {
            return TimeZone.CurrentTimeZone.GetDaylightChanges(Year).End;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Year"></param>
        /// <returns></returns>
        public static TimeSpan GetDaylightTimeDelta(int Year)
        {
            return TimeZone.CurrentTimeZone.GetDaylightChanges(Year).Delta;
        }
    }
}
